package sh.base.aop.util;

import sh.base.aop.FileOperationException;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

/**
 * @author lqq
 * @date 2019/2/25
 */
public class FileUtil {

    /**
     * 获取指定路径下的配置文件
     * @param path
     * @return
     */
    public static Properties getProperties(String path) {
        //从classpath下加载
        Properties properties = new Properties();
        //获取classpath的路径，然后加上当前路径
        URL url = FileUtil.class.getClassLoader().getResource("");
        url.getPath();
        // 使用properties对象加载输入流
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(url.getPath() + path));
            properties.load(bufferedReader);
        } catch (IOException e) {
            e.printStackTrace();
            throw new FileOperationException("加载文件流异常" + e.getMessage());
        }
        return properties;
    }
}
